package com.bitcoin.scheduler.task;

import com.bitcoin.scheduler.domain.Bcc24HourVolume;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Market {
    @Test
    public void test() throws IOException {
        String listings = Jsoup
            .connect("https://api.coinmarketcap.com/v2/listings/")
            .ignoreContentType(true)
            .execute().body();
        Map coinMap = new ObjectMapper().readValue(listings, Map.class);
        Map<String, String> mapping = Maps.newConcurrentMap();
        ArrayList<Map> coinList = (ArrayList) MapUtils.getObject(coinMap, "data");
        for (Map coin : coinList) {
            mapping.put(MapUtils.getString(coin, "id"), MapUtils.getString(coin, "symbol"));
        }
        Document document = Jsoup
            .connect("https://coinmarketcap.com/currencies/volume/24-hour")
            .cookie("currency", "CNY")
            .get();

        Elements elements = document.select(".table-responsive > table > tbody > tr");
        String currentSymbol = null;
        List<Bcc24HourVolume> result = Lists.newArrayList();
        for (Element element : elements) {
            String bccName = element.attr("id");
            if (StringUtils.isNotEmpty(bccName)) {
                Element logo = element.select("td > h3 > img").first();
                String url = logo.attr("data-src");
                String[] urlSplit = StringUtils.split(url, "/");
                String[] last = StringUtils.split(urlSplit[urlSplit.length - 1], ".");
                currentSymbol = MapUtils.getString(mapping, last[0]);
            }
            else {
                Elements elementsTd = element.select("td");
                if (currentSymbol == null || elementsTd.size() != 6) {
                    continue;
                }
                Bcc24HourVolume volume = new Bcc24HourVolume();
                volume.setSymbol(currentSymbol);
                volume.setSource(elementsTd.get(1).text());
                volume.setPair(elementsTd.get(2).text());
                volume.setVolumeUsd(elementsTd.get(3).text());
                volume.setPriceUsd(elementsTd.get(4).text());
                volume.setVolumePercent(elementsTd.get(5).text());
                volume.setId(volume.getSource() + "_" + volume.getPair());
                result.add(volume);
            }
        }
        System.out.println(result);


    }
}
